package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ITheTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 考试管理
 *
 * @author py
 * @Date 2018年2月4日23:05:54
 */
@Controller
@RequestMapping("/theTest")
public class TheTestController extends BaseController {

    @Autowired
    ITheTestService theTestService;

    /**
     * 跳转培训页
     */
    @RequestMapping("")
    public String theTest(Model model) {
        return "/theTest/theTest.html";
    }

    /**
     * 跳转到添加考试页面
     */
    @RequestMapping("/theTest_add")
    public String toTheTestAdd(Model model) {
        return "/theTest/theTest_add.html";
    }

    /**
     * 跳转到修改考试页面
     */
    @RequestMapping("/theTest_update/{theTestId}")
    public String toTheTestUpdate(@PathVariable String theTestId, Model model) throws Exception {
        if (ToolUtil.isEmpty(theTestId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        TheTestInfo theTestInfo = theTestService.getTheTestOne(theTestId);
        model.addAttribute("theTestInfo", theTestInfo);
        return "/theTest/theTest_edit.html";
    }

    /**
     * 添加考试
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public int theTestAdd(@Valid TheTestInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String theTestName = info.getTheTestName().trim();
        int testNameCount = theTestService.selectSameTestNameCount(theTestName);
        if (testNameCount > 0) {
            return 0;
        } else {
            return theTestService.insert(info);
        }
    }

    /**
     * 修改考试
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(@Valid TheTestInfo entity, BindingResult result) throws Exception {
        int testNameCount = theTestService.selectSameTestNameCountForUpdate(entity.getId(), entity.getTheTestName());
        if (testNameCount > 0) {
            return 0;
        } else {
            if (result.hasErrors()) {
                throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
            }
            entity.setUpdatePeople(ShiroKit.getUser().getName());
            entity.setUpdateTime(DateTimeKit.now());
            entity.setTotalScore(entity.getNumberOfQuestion() * entity.getScoresOfQuestion());
            theTestService.UpdateWithTheTestInfo(entity);
            return 1;
        }
    }

    /**
     * 删除考试
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Tip delete(@PathVariable String id) throws Exception {
        theTestService.delete(id);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到考试列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchTheTestInfo searchTheTestInfo, HttpServletRequest request) {
        Page<TheTestInfo> page = new PageFactory<TheTestInfo>().defaultPage();
        try {
            List<TheTestInfo> theTestInfoList = theTestService.selectPageListByQueryGetMap(page, searchTheTestInfo);
            page.setRecords(theTestInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }


}
